package com.lagou.code.service.impl;

import com.lagou.code.bean.AuthCode;
import com.lagou.code.dao.AuthCodeRepository;
import com.lagou.code.service.CodeService;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;

@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    AuthCodeRepository authCodeRepository;

    @Override
    public Integer validateCode(String email, String code) {
        AuthCode authCode = authCodeRepository.findByEmail(email);

        if(authCode == null || authCode.getExpiretime() ==null ||
                authCode.getExpiretime().compareTo(LocalDateTime.now()) < 0){
            return 2;
        }

        return StringUtils.isEquals(authCode.getCode(), code) ? 0 : 1;
    }
}
